#include "widget.h"
#include "ui_widget.h"
#include<QtDebug>
#include<QRegExpValidator>
#include<QRegExp>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->pushButton->setEnabled(false);


    ui->lineEdit_name->setPlaceholderText("请输入你的名字");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_password->setPlaceholderText("请输入你的密码");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

    ui->lineEdit_password_confirm->setPlaceholderText("请重新确认密码");
    ui->lineEdit_password_confirm->setClearButtonEnabled(true);
    ui->lineEdit_password_confirm->setEchoMode(QLineEdit::Password);

    ui->lineEdit_phone->setPlaceholderText("请输入你的'1'开头的电话号码");
    ui->lineEdit_phone->setClearButtonEnabled(true);
//    ui->lineEdit_phone->setInputMask("000 0000 0000");
    //给lineEdit注册一个validator正则表达式
    ui->lineEdit_phone->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString s=ui->radioButton_male->isChecked()?"男":"女";
    qDebug()<<"名字"<<ui->lineEdit_name->text()
           <<"密码"<<ui->lineEdit_password->text()
          <<"性别"<<s
         <<"电话号码"<<ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    qDebug()<<arg1;
    QString content=arg1;
    int pos=0;
    if(ui->lineEdit_phone->validator()->validate(content,pos)==QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
        ui->label_5->setText("");
    }
    else
    {
        if(content.isEmpty())
            ui->label_5->setText("手机号码输入的格式不对");
        ui->label_5->setStyleSheet("color:red");
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_lineEdit_password_textEdited(const QString &arg1)
{
    (void)arg1;
    const QString s1=ui->lineEdit_password->text();
    const QString s2=ui->lineEdit_password_confirm->text();
    if(s1.isEmpty() &&s2.isEmpty())
    {
        ui->label_password->setText("密码为空");
        ui->label_password->setStyleSheet("color:red");
    }
    else if(s1==s2)
    {
        ui->label_password->setText("两次密码都正确了");
        ui->label_password->setStyleSheet("color:green");
    }
    else
    {
        ui->label_password->setText("两次密码不一致");
        ui->label_password->setStyleSheet("color:red");
    }
}

void Widget::on_lineEdit_password_confirm_textEdited(const QString &arg1)
{
    (void)arg1;
    const QString s1=ui->lineEdit_password->text();
    const QString s2=ui->lineEdit_password_confirm->text();
    if(s1.isEmpty() &&s2.isEmpty())
    {
        ui->label_password->setText("密码为空");
        ui->label_password->setStyleSheet("color:red");
    }
    else if(s1==s2)
    {
        ui->label_password->setText("两次密码都正确了");
        ui->label_password->setStyleSheet("color:green");
    }
    else
    {
        ui->label_password->setText("两次密码不一致");
        ui->label_password->setStyleSheet("color:red");
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_checkBox_2_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password_confirm->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password_confirm->setEchoMode(QLineEdit::Password);
    }
}
